import React from 'react'
import ReactDOM from 'react-dom'   
import './css/ResponsivePopup.css'
import * as DataHandler from './DataHandler'
import  Popup from '../../component/Popup/Popup'//Popup
 
class ResponsivePopup extends React.Component {
    constructor(props) {
        super(props);
        this.state = {  
        };
    }
     urlpar ={}; 
    componentWillMount = () => {  

     
    } 
    componentDidMount = () => {   
  
            this.urlpar ={   id:1,      type:'adjust'       } 
          this.showModal()
     
    } 

    info={
        boxW:360,
    }
 
    showModal = () => {
        this.refs.popup.showModal() ; 
        if(this.urlpar.type=='close'){//关闭
            this.popupConfig.defaultTabsIndex=0;
             this.refs.popup.config.tabsClick( this.refs.popup.config.tabsDatas[0],0);
        }else{//调整 adjust
            this.popupConfig.defaultTabsIndex=1; 
            this.refs.popup.config.tabsClick( this.refs.popup.config.tabsDatas[1],1);
            //  this.refs.popup.config.data[1].data[0].btnClick(    this.refs.popup.config.data[1].data[0],0);
        }
    }
    // 弹窗配置信息
    popupConfig = {
        title:'响应类型及级别',
        visible:false,//弹窗的关闭打开        
        defaultTabsIndex:0,//默认点击一个（关闭响应）
         nextFn : () => { //点击保存
            setTimeout(() => {
                let that =this;
                this.refs.popup.config.visible =false ;
                let index =this.refs.popup.config.defaultTabsIndex; 
                let par={}
                let tempId=[];
                let tempMob=[];
                 this.refs.popup.config.data[index].data[3].data.map((item,i)=>{
                     tempId.push(item.id)
                     tempMob.push(item.mobile)
                 })
                switch(index){
                    case 0://关闭
                         par={
                            status:  this.refs.popup.config.data[index].data[0].currentLev=='是'?2:1,//应急响应状态,1:执行中 2：结束
                            close_content: this.refs.popup.config.data[index].data[1].content,//关闭应急响应通知内容
                            end_time: this.refs.popup.config.data[index].data[2].showTime,//结束时间
                            close_recive_uids: tempId.toString(),//关闭应急响应通知对象编号
                            id:this.urlpar.id 
                        }
                           T.ajax({//调整等级
                            key: 's_emergency_response_record',
                            data: par,
                            method: 'PUT',
                            success: function success(result) {
                                let smspar={
                                    sms_content:par.close_content,
                                    phone:tempMob,
                                }

                                T.ajax({//发送短信
                                    key: 'sendsms',
                                    data: smspar,
                                    method: 'POST',
                                    success: function success(result) { 
                                        that.refs.popup.config.closeFn() ;  
                                        T.data('popup','hide'); 
                                        T.router.go('/Organizational_Structure');
                                        T.data('yjxy_refresh', {yjxy_refresh: true})
                                    }
                                });
                                
                            }
                        });
                        break;
                    case 1://调整
                          par={
                            warning_level:  this.refs.popup.config.data[index].data[0].currentLev,//应急响应级别
                            start_content: this.refs.popup.config.data[index].data[1].content,//开启应急响应通知内容
                            begin_time: this.refs.popup.config.data[index].data[2].showTime,//开始时间
                            start_recive_uids: tempId.toString() ,//开启应急响应通知对象编号
                             id:this.urlpar.id 
                        }

                       T.ajax({//调整等级
                            key: 's_emergency_response_record',
                            data: par,
                            method: 'PUT',//更新
                            success: function success(result) {
                                let smspar={
                                    sms_content:par.start_content,
                                    phone:tempMob,
                                }

                                T.ajax({//发送短信
                                    key: 'sendsms',
                                    data: smspar, 
                                    method: 'POST',//创建 
                                    success: function success(result) { 
                                        that.refs.popup.config.closeFn() ;
                                        T.data('popup','hide');  
                                        T.router.go('/Organizational_Structure');
                                        T.data('yjxy_refresh', {yjxy_refresh: true})
                                    }
                                });
                                
                            }
                        });
                        break;
                }
                 
 
            }, 2000);
        }, 

    }
 
 
    render() { 
        return (
            <div  className="ResponsivePopup">
               <div type="primary" onClick={this.showModal}>
                Open
                </div>
                 <Popup  ref='popup' config = {this.popupConfig}  ></Popup> 
            </div>
        )
    }
} 
export default ResponsivePopup 